package com.productline;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

import org.jdom.Document;
import org.jdom.Element;

public class SensorConfigurePanel extends JPanel {

	public Document doc;
	public HashMap<String, JPanel> map = new HashMap<String, JPanel>();

	public SensorConfigurePanel(Document doc) {
		Element root = doc.getRootElement();
		List<Element> sensors = root.getChild("sensors").getChildren();

		this.setLayout(null);
		Border border = BorderFactory
				.createTitledBorder("Sensor Configuration: ");
		this.setBorder(border);

		for (Element e : sensors) {
			JPanel p = new JPanel(null);
			JPanel textPanel = new JPanel(new GridLayout(6, 2));
			JLabel l = new JLabel(e.getChild("name").getText(), JLabel.LEFT);
			l.setBounds(20, 0, 200, 30);
			p.add(l);

			// JLabel n = new JLabel("Number: ", JLabel.CENTER);
			// textPanel.add(n);
			// JTextField field = new JTextField(10);
			// field.setBackground(Color.white);
			// field.setEditable(true);
			// field.setText(e.getChild("number").getText());
			// field.addFocusListener(new
			// MyFocusListener(field,e.getChild("number")));
			// textPanel.add(field);

			// JPanel textPanel2 = new JPanel(new GridLayout(3,2));
			JLabel n2 = new JLabel("High Resolution Number: ", JLabel.CENTER);
			textPanel.add(n2);
			JTextField field2 = new JTextField(10);
			field2.setBackground(Color.white);
			field2.setEditable(true);
			field2.setText(e.getChild("highResolutionNumber").getText());
			field2.addFocusListener(new MyFocusListener(field2, e.getChild("highResolutionNumberName").getText()));
			textPanel.add(field2);

			// JPanel textPanel3 = new JPanel(new GridLayout(3,2));
			JLabel n3 = new JLabel("Low Resolution Number: ", JLabel.CENTER);
			textPanel.add(n3);
			JTextField field3 = new JTextField(10);
			field3.setBackground(Color.white);
			field3.setEditable(true);
			field3.setText(e.getChild("lowResolutionNumber").getText());
			field3.addFocusListener(new MyFocusListener(field3, e.getChild(
					"lowResolutionNumberName").getText()));
			textPanel.add(field3);

			JLabel n4 = new JLabel("High Resolution Weight: ", JLabel.CENTER);
			textPanel.add(n4);
			JTextField field4 = new JTextField(10);
			field4.setBackground(Color.white);
			field4.setEditable(true);
			field4.setText(e.getChild("highWeight").getText());
			field4.addFocusListener(new MyFocusListener(field4, e.getChild(
					"highWeightName").getText()));
			textPanel.add(field4);

			JLabel n6 = new JLabel("Low Resolution Weight: ", JLabel.CENTER);
			textPanel.add(n6);
			JTextField field6 = new JTextField(10);
			field6.setBackground(Color.white);
			field6.setEditable(true);
			field6.setText(e.getChild("lowWeight").getText());
			field6.addFocusListener(new MyFocusListener(field6, e.getChild(
					"lowWeightName").getText()));
			textPanel.add(field6);

			JLabel n7 = new JLabel("Sensor Period: ", JLabel.CENTER);
			textPanel.add(n7);
			JTextField field7 = new JTextField(10);
			field7.setBackground(Color.white);
			field7.setEditable(true);
			field7.setText(e.getChild("period").getText());
			// System.out.println(field7.getText());
			field7.addFocusListener(new MyFocusListener(field7, e.getChild(
					"periodName").getText()));
			textPanel.add(field7);
			// textPanel.setBounds(10,50,300,100);

			JLabel n8 = new JLabel("Sensor History Length: ", JLabel.CENTER);
			textPanel.add(n8);
			JTextField field8 = new JTextField(10);
			field8.setBackground(Color.white);
			field8.setEditable(true);
			field8.setText(e.getChild("history").getText());
			field8.addFocusListener(new MyFocusListener(field8, e.getChild(
					"historyName").getText()));
			textPanel.add(field8);
			textPanel.setBounds(10, 50, 300, 100);

			p.add(textPanel);
			p.setBounds(10, 30, 350, 150);
			this.add(p);
			p.setVisible(false);

			map.put(e.getChild("name").getText(), p);
		}
		JPanel p = new JPanel(null);
		JPanel textPanel = new JPanel(new GridLayout(6, 2));

		Element positionSensor = root.getChild("position-sensor");
		JLabel l = new JLabel(positionSensor.getChild("name").getText(),
				JLabel.LEFT);
		l.setBounds(20, 0, 200, 30);
		p.add(l);

		JLabel n2 = new JLabel("Position Sensor Type: ", JLabel.CENTER);
		textPanel.add(n2);

		ButtonGroup g = new ButtonGroup();
		String defaultType = positionSensor
				.getChildText("defaultPositionSensorType");
		for (Object e : positionSensor.getChildren("positionSensorType")) {
			String name = ((Element) e).getChildText("name");
			JRadioButton b = new JRadioButton(name,
					name.equals(defaultType) ? true : false);
			g.add(b);
			textPanel.add(b);

			b.addActionListener(new SensorActionListener(name, 0,0,positionSensor
					.getChildText("ifSelected"), GeneratorFrame.picture,
					GeneratorFrame.generatorPanel));
		}
		textPanel.setBounds(10, 50, 300, 100);

		p.add(textPanel);
		p.setBounds(10, 30, 350, 150);
		this.add(p);
		p.setVisible(false);
		map.put(positionSensor.getChild("name").getText(), p);

	}
}